#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/10/31 14:30
# @Author  : Triste_hui


from game_db import *
from player import *
import os
import random


# 加载游戏信息
def gameload():
    name = input('请输入您的角色名称:')
    if name != "q":
        result = game.get_user(name)
        while not result:
            name = input('未查询到该角色名称，请检查重新输入:')
            if name == "q":
                return False
            result = game.get_user(name)
        player.load(**result)
        return True
    else:
        return False

# 新建角色
def newgame():
    os.system('cls')
    name = input('请输入角色名称:')
    if name != "q":
        while not game.new_user(name):
            name = input('角色已存在，请重新输入:')
            if name == "q":
                return False
        result = game.get_user(name)
        player.load(**result)
        print('\'%s\'角色创建成功' % name)
        print("新建角色默认赠送10000金币钻石！！！")
        return True
    else:
        return False


# 游戏开始执行
def gamestart():
    while True:
        print("欢迎来到卡牌游戏~")
        print("1.读取存档" + '\n' + "2.新建人物" + "\n" + "q.退出游戏")
        choice = input('请选择:')
        if choice != 'q':
            while (not choice) or (choice != '1' and choice != '2'):
                print("输入错误!")
                choice = input('请重新输入:')
            if choice == "1":
                if gameload():
                    break
            elif choice == "2":
                if newgame():
                    break
            elif choice == "q":
                os._exit(1)
            os.system('cls')
        else:
            os._exit(1)


# 抽卡
def drawcard():
    qualitys = {
        1: "白色",
        2: "绿色",
        3: "蓝色",
        4: "紫色",
        5: "橙色",
        6: "神"
    }
    print("您当前有%d金币，%d钻石。" % (player.getGold(), player.getDiamond()))
    print("1.金币抽奖(100金币一次)\n2.钻石抽奖(100钻石一次)\nq.退出")
    choice= input("请选择抽奖方式:")

    while choice != 'q':
        if choice == "1":
            player.gold -= 100
            print('抽奖开始-----')
            r_num = random.randint(1,1000)# 概率千分制
            if r_num < 50:
                q = 4
            elif r_num < 200:
                q = 3
            elif r_num < 500:
                q = 2
            else :
                q = 1
            result = game.draw_card(q,)
            quality = qualitys[result['quality']]
            print('恭喜你抽到了[%s卡：%s]' % (quality, result['name']))
            print("攻击力：%d" % result['base_atk'])
            print("防御力：%d" % result['base_def'])
            break



def view():
    print("-" * 5 + "游戏开始" + "-" * 5)
    print("%s，你好，欢迎来到卡牌游戏" % player.getName())
    while True:
        print("请选择接下来的操作")
        print("1.抽卡\n2.查看属性\nq.退出游戏")
        choice = input("请选择:")
        if choice == 'q':
            break
        elif choice == '1':
            drawcard()



game = Game()
player = Player()
gamestart()
view()
